﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json.Linq;
using System.Data;
using System.Data.SqlClient;

namespace RoadFlow.IntegrateTest.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
 
        #region API测试页面

        public ActionResult TestFlow()
        {
            string instanceId = Request.QueryString["instanceid"];
            string f1 = string.Empty;
            string f2 = string.Empty;
            if (!string.IsNullOrWhiteSpace(instanceId))
            {
                Common.DBHelper dBHelper = new Common.DBHelper();
                DataTable dt = dBHelper.GetDataTable("select * from rf_test where id='" + instanceId + "'");
                if (dt.Rows.Count > 0)
                {
                    f1 = dt.Rows[0]["f1"].ToString();
                    f2 = dt.Rows[0]["f1"].ToString();
                }
            }
            ViewData["f1"] = f1;
            ViewData["f2"] = f2;
            return View();
        }

        public ActionResult FlowRun()
        {
            string flowId = Request.QueryString["flowid"];
            string stepId = Request.QueryString["stepid"];
            string taskId = Request.QueryString["taskid"];
            string instanceId = Request.QueryString["instanceid"];
            string groupId = Request.QueryString["groupid"];
            var address = Common.Tools.GetStepFormAddress(Guid.Parse(flowId), Guid.Parse(stepId));
            return Redirect(address.Item1 + Request.Url.Query);
        }

        /// <summary>
        /// 流程处理
        /// </summary>
        /// <returns></returns>
        public string ExecuteFlow()
        {
            string url = Common.Tools.ApiAddress + "/RoadFlowCoreApi/FlowTask/ExecuteTask";
            string flowId = Request["flowid"];
            string taskId = Request.QueryString["taskid"];
            string type = Request.Form["type"];
            string f1 = Request.Form["f1"];
            string f2 = Request.Form["f2"];
            string instanceId = Request.QueryString["instanceid"];
            string steps = Request.Form["steps"];
            //保存业务数据
            if ("save".Equals(type))
            {
                DataTable dt = null;
                Common.DBHelper dBHelper = new Common.DBHelper();
                if (!string.IsNullOrWhiteSpace(instanceId))
                {
                    dt = dBHelper.GetDataTable("select * from rf_test where id='" + instanceId + "'");
                }
                string sql = string.Empty;
                if (null == dt)
                {
                    sql = "insert into rf_test(id,f1,f2) values(@id,@f1,@f2)";
                    instanceId = Guid.NewGuid().ToString();
                }
                else
                {
                    sql = "update rf_test set f1=@f1,f2=@f2 where id=@id";
                }
                SqlParameter[] sqlParameters = new SqlParameter[] {
                    new SqlParameter("@id", instanceId),
                    new SqlParameter("@f1",f1),
                    new SqlParameter("@f2",f2)
                };
                dBHelper.Execute(sql, sqlParameters);
            }

            //调用处理流程API
            JObject jObject = new JObject
            {
                { "systemcode", Common.Tools.SystemCode },
                { "flowid", flowId },
                { "type", type },
                { "senderid", Common.Tools.CurrentUserId },
                { "taskid", taskId },
                { "tasktitle", f1 },
                { "instanceid", instanceId },
                { "steps", string.IsNullOrWhiteSpace(steps) ? new JArray() : JArray.Parse(steps)}
            };
            string json = Common.HttpHelper.SendPost(url, jObject.ToString());
            return json;
        }

        public ActionResult Send()
        {
            string url = Common.Tools.ApiAddress + "/RoadFlowCoreApi/Flow/GetSendSteps";
            string flowId = Request.QueryString["flowid"];
            string stepId = Request.QueryString["stepid"];
            string taskId = Request.QueryString["taskid"];
            JObject jObject = new JObject
            {
                { "systemcode", Common.Tools.SystemCode },
                { "userid", Common.Tools.CurrentUserId },
                { "flowid", flowId },
                { "stepid", stepId },
                { "taskid", taskId },
                { "freesend", false }
            };
            string json = Common.HttpHelper.SendPost(url, jObject.ToString());
            JObject jObject1 = JObject.Parse(json);
            if (jObject1.Value<int>("errcode") != 0)
            {
                Response.Write(jObject1.Value<string>("errmsg"));
                Response.End();
            }
            ViewData["json"] = jObject1.Value<JArray>("data");
            return View();
        }

        public ActionResult Back()
        {
            string url = Common.Tools.ApiAddress + "/RoadFlowCoreApi/Flow/GetBackSteps";
            string flowId = Request.QueryString["flowid"];
            string stepId = Request.QueryString["stepid"];
            string taskId = Request.QueryString["taskid"];
            JObject jObject = new JObject
            {
                { "systemcode", Common.Tools.SystemCode },
                { "userid", Common.Tools.CurrentUserId },
                { "taskid", taskId }
            };
            string json = Common.HttpHelper.SendPost(url, jObject.ToString());
            JObject jObject1 = JObject.Parse(json);
            if (jObject1.Value<int>("errcode") != 0)
            {
                Response.Write(jObject1.Value<string>("errmsg"));
                Response.End();
            }
            ViewData["json"] = jObject1.Value<JArray>("data");
            return View();
        }

        public ActionResult WaitTasks()
        {
            return WaitTasks(null);
        }

        /// <summary>
        /// 显示处理过程
        /// </summary>
        /// <returns></returns>
        public ActionResult ShowTasks()
        {
            string url = Common.Tools.ApiAddress + "/RoadFlowCoreApi/FlowTask/GetGroupTasks";
            JObject jObject = new JObject
            {
                { "systemcode", Common.Tools.SystemCode },
                { "groupid", Request.QueryString["groupid"] }
            };
            string json = Common.HttpHelper.SendPost(url, jObject.ToString());
            ViewData["json"] = json;
            return View();
        }

        /// <summary>
        /// 待办列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult WaitTasks(FormCollection formCollection)
        {
            string url = Common.Tools.ApiAddress + "/RoadFlowCoreApi/FlowTask/GetWaitTasks";
            string tasktitle = Request.Form["tasktitle"];
            string flowid = Request.Form["flowid"];
            if (string.IsNullOrWhiteSpace(flowid))
            {
                flowid = Common.Tools.GetFlowIds();
            }
            JObject jObject = new JObject
            {
                { "systemcode", Common.Tools.SystemCode },
                { "userid", Common.Tools.CurrentUserId },
                { "flowid", flowid },
                { "title", tasktitle },
                { "pagesize", 100 }
            };
            string json = Common.HttpHelper.SendPost(url, jObject.ToString());
            ViewData["json"] = json;
            ViewData["flowOptions"] = Common.Tools.GetFlowOptions(Request.Form["flowid"]);
            ViewData["taskTitle"] = tasktitle;
            return View();
        }

        /// <summary>
        /// 已完成列表
        /// </summary>
        /// <returns></returns>
        public ActionResult CompletedTasks()
        {
            return CompletedTasks(null);
        }

        /// <summary>
        /// 待办列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CompletedTasks(FormCollection formCollection)
        {
            string url = Common.Tools.ApiAddress + "/RoadFlowCoreApi/FlowTask/GetCompletedTasks";
            string tasktitle = Request.Form["tasktitle"];
            string flowid = Request.Form["flowid"];
            if (string.IsNullOrWhiteSpace(flowid))
            {
                flowid = Common.Tools.GetFlowIds();
            }
            JObject jObject = new JObject
            {
                { "systemcode", Common.Tools.SystemCode },
                { "userid", Common.Tools.CurrentUserId },
                { "flowid", flowid },
                { "title", tasktitle },
                { "pagesize", 100 }
            };
            string json = Common.HttpHelper.SendPost(url, jObject.ToString());
            ViewData["json"] = json;
            ViewData["flowOptions"] = Common.Tools.GetFlowOptions(Request.Form["flowid"]);
            ViewData["taskTitle"] = tasktitle;
            return View();
        }

        #endregion
    }
}